#!/usr/bin/env python
#-*- coding:UTF-8 -*-
#####################################################
# Author: Jiaion - jiaion21@gmail.com
# Last modified:	2011-04-23 16:15
# Filename:	device.py
#####################################################
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from Context.Render import render
from django.contrib.auth.decorators import login_required
from django.utils import simplejson
from Jiaion.models import Device
from django.conf import settings

@login_required()
def Devview(request):
    return render('device.html',{},request)

@login_required()
def Seadev(request):
    if request.method == 'POST':
       IP = request.REQUEST.get('IP')
       if IP :
           Results = Device.objects.filter(Ip=IP)
           return render('seadev.html',{'results':Results,},request)
       else:
           Results = Device.objects.all()
           return render('seadev.html',{'results':Results,},request)
    return render('seadev.html',{},request)

@login_required()
def Adddev(request):
    if request.method == 'POST':
        INFO = {}
        Tuple = ('IP','CPU','MEM','DISK','POSITION','STATE','SERVER','OWNER')
        for i in Tuple:
            INFO[i] = request.REQUEST.get(i)
        DBDA = Device(Ip=INFO['IP'],Cpu=INFO['CPU'],Mem=INFO['MEM'],Disk=INFO['DISK'],
                      Position=INFO['POSITION'],State=INFO['STATE'],Server=INFO['SERVER'],Owner=INFO['OWNER'])
        DBDA.save()
        return render('adddev.html',{'DBDA':INFO,},request) 
    return render('adddev.html',{},request)

@login_required()
def Deldev(request):
    if request.method == 'POST':
        IP = request.REQUEST.get('IP')
        DLID = request.REQUEST.get('DLID')
        if DLID and not IP:
            Results = Device.objects.get(id=DLID)
            Results.delete()
            Resultss = Device.objects.all()
            return render('deldev.html',{'results':Resultss,},request)
        if IP :
            Results = Device.objects.filter(Ip=IP)
            return render('deldev.html',{'results':Results,},request)
        Results = Device.objects.all()
        return render('deldev.html',{'results':Results,},request)
    return render('deldev.html',{},request)

@login_required()
def Chadev(request):
    if request.method == 'POST':
        IP = request.REQUEST.get('IP')
        MDID = request.REQUEST.get('MDID')
        Change = request.REQUEST.get('Change')
        if IP and MDID and Change:
            INFO = {}
            Tuple = ('ID','IP','CPU','MEM','DISK','POSITION','STATE','SERVER','OWNER')
            for i in Tuple:
                INFO[i] = request.REQUEST.get(i)
            DBDA = Device(id=MDID,Ip=INFO['IP'],Cpu=INFO['CPU'],Mem=INFO['MEM'],Disk=INFO['DISK'],
                   Position=INFO['POSITION'],State=INFO['STATE'],Server=INFO['SERVER'],Owner=INFO['OWNER'])
            DBDA.save()
            return render('chadev.html',{'Change':'OK',},request) 
        if IP and not MDID:
            Results = Device.objects.filter(Ip=IP)
            return render('chadev.html',{'results':Results,},request)
        elif MDID :
            Results = Device.objects.get(id=MDID)
            return render('chadev.html',{'MDID':Results,},request)
        else:
            Results = Device.objects.all()
            return render('chadev.html',{'results':Results,},request)
    return render('chadev.html',{},request)

